﻿Public Class level2
    Dim xsoal As String
    Dim colorTemp As String
    Property color As String
    Public Property Soal As String
        Get
            Return xSoal
        End Get
        Set(ByVal value As String)
            xsoal = value
        End Set
    End Property

    Private Sub level2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RefreshSoal()
    End Sub
    Sub resetColor()
        For Each j As Object In Me.Controls
            If TypeOf j Is BtnSoal Then
                If j.status = False Then
                    j.resetColor() 
                End If
            End If
        Next
        resetColorJawab()
    End Sub
    Sub resetColorJawab()
        For Each j As Object In Me.Controls
            If TypeOf j Is BtnJwb Then
                If j.status = False Then
                    j.resetColor()
                End If
            End If
        Next
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim s As Integer = 0
        For Each j As Object In Me.Controls
            If TypeOf j Is BtnSoal Or TypeOf j Is BtnJwb Then
                If j.statusColor = True Then
                    s = s + 1
                End If
            End If
        Next

        Dim tJawab As String = ""

        If s Mod 2 = 0 Then
            For Each l As Object In Me.Controls
                If TypeOf l Is BtnJwb Then
                    If l.statusColor = True And l.status = False Then
                        tJawab = l.label
                    End If
                End If
            Next
            For Each k As Object In Me.Controls
                If TypeOf k Is BtnSoal Then
                    If k.statusColor = True And k.status = False Then
                        If k.jawab() = tJawab Then
                            jtrue()
                            MsgBox("Answer True")
                        Else
                            MsgBox("Answer Wrong")
                        End If
                    End If
                End If
            Next
            
        End If
        If s = 8 Then
            Button2.Visible = True
            Button1.Visible = False
        End If
    End Sub
    Private Sub jtrue()
        For Each j As Object In Me.Controls
            If TypeOf j Is BtnSoal Or TypeOf j Is BtnJwb Then
                If j.statusColor = True And j.status = False Then
                    j.status = True
                End If
            End If
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        RefreshSoal()
    End Sub

    Sub randomizeArray(ByVal items() As String)
        Dim mIndex As Integer = items.Length - 1
        Dim rnd As New Random
        For i As Integer = 0 To mIndex
            Dim j As Integer = rnd.Next(i, mIndex + 1)
            Dim temp As String = items(i)
            items(i) = items(j)
            items(j) = temp
        Next
    End Sub
    Private Sub RefreshSoal()
        Button1.Visible = True
        Button2.Visible = False
        Dim soal() As String = Split(xsoal, vbCrLf)
        Dim carray As Integer = UBound(soal)
        Dim xLabelSoal(carray) As String
        Dim xJawaban(carray) As String
        Dim lJawaban(carray) As String

        randomizeArray(soal)

        Dim csoal(3) As String
        For i As Integer = 0 To 3
            Dim xTemp() As String = Split(soal(i), ";")
            csoal(i) = soal(i)
            xLabelSoal(i) = xTemp(0)
            xJawaban(i) = xTemp(1)
        Next

        randomizeArray(csoal)

        For i As Integer = 0 To 3
            Dim lTemp() As String = Split(csoal(i), ";")
            lJawaban(i) = lTemp(1)
        Next
        Dim k As Integer = 0
        Dim l As Integer = 0
        Dim color() As String = {"blue", "orange", "green", "yellow"}

        For Each j As Object In Me.Controls
            If TypeOf j Is BtnSoal Then
                j.refreshText(xLabelSoal(k))
                j.color = color(k)
                j.jawab = xJawaban(k)
                j.status = False
                j.statusColor = False
                j.resetColor()
                k += 1
            End If
            If TypeOf j Is BtnJwb Then
                j.refreshText(lJawaban(l))
                j.label() = lJawaban(l)
                j.status = False
                j.statusColor = False
                j.resetColor()
                l += 1
            End If
        Next
    End Sub
End Class
